<template>
	<view>
		<!-- 标题 -->
		<view class="login-top">
			<!-- logo -->
			<view class="login-top-image">
				<image :src="getStaticFilePath('/login/logo.png')"></image>
			</view>
			<!-- 名字 -->
			<view class="login-top-title">
				飞砂走石
			</view>
			<!-- 平台 -->
			<view class="login-top-title-text">
				客户在线下单采购平台
			</view>
		</view>
		<view class="login-content">
			<view class="login-li wx-nickName">
				<view class="weui-input">
					<input v-model="nickName" type="nickname" @blur="onNicknameBlur" placeholder="请输入昵称"/>
				</view>
			
			</view>
			<view class="login-li">
				<button style="background-color:#32CD32; color: #ffffff;width: 100%;height: 35px;line-height: 35px;" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" size="mini" type="success"
					shape="circle">
					手机号登陆
				</button>
				<!-- <text class="iconfont iconshouji3"></text>
				<input v-model="iphone" type="number" maxlength="11" value="" /> -->

			</view>
			<!-- <view class="login-li">
				<text class="iconfont icona-yanzhengma1"></text>
				<input v-model="code" type="number" maxlength="6" value="" />
				<view @click="getCode">
					{{codetext}}
				</view>
			</view> -->
		</view>

		<!-- <view class="login-button">

			<button class="login-content-btn" @click="submitLogin" :class="btncolor" type="primary">绑定</button>
		</view> -->

		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timeOut: 60,
				token: '',
				iphone: '',
				code: '',
				yqm: '',
				codetext: '获取验证码',
				ErpUserTypeText: '',
				cid: uni.getStorageSync('cid'),
				nickName:'',
				codewx: '',
				sessionKey: '',
				userInfo: {},

			}
		},
		computed: {
			btncolor() {
				var i = ''
				var c = ''
				if (this.iphone && this.iphone != '') {
					i = this.iphone.trim()
				}
				if (this.code && this.code != '') {
					c = this.code.trim()
				}
				if (i.length == 11 && (c.length == 6 || c.length == 4)) {
					return ''
				} else {
					return 'login-content-btn-active'
				}
			}
		},
		onLoad(ops){
			this.nickName = decodeURIComponent(ops.nickName)
			console.log(ops)
		},
		onShow() {
			this.getUserProfile()
			this.getToken()
			// this.hqwxuse()
			// if(this.nickName =='微信用户'){
			// 	this.nickName = ''
			// }

		},
		onLaunch: function() {},
		methods: {

			getUserProfile() {
				let code = "";
				let that = this //保存当前作用域

				uni.login({ //直接用这个调用微信接口
					onlyAuthorize: true,
					provider: 'weixin',
					success: function(response) { // 用微信登录的话就要去微信开发工具
						console.log(response) //这里打印就说明接口调用成功了，然后看message login ：ok
						//微信登录就完了，后面就是获取用户信息
						uni.request({
							url: that.baseUrl + '/system/getOpenId/',
							method: 'POST',
							dataType: 'json',
							data: {
								code: response.code,
							},
							success(response) {
								console.log(response)
								if (response.errMsg == 'request:ok') {
									// that.sessionKey = response.data.data.sessionKey
									that.openid = response.data.data.openid
									uni.setStorageSync('openid', that.openid)
								}

							}
						})

					}
				})
			},
			onNicknameBlur(e) {
				this.nickName = e.detail.value;
			},
			getPhoneNumber(e) {
				console.log(e)
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					// console.log('用户点击了接受');
					// console.log(e);
					// console.log(e);
					// console.log(e.detail.code)
					console.log(this.nickName)
					if(this.nickName ==''){
						this.$refs.uToast.show({
							title: '请输入昵称',
							type: 'default',
							icon: false
						})
						return
					}
					
					this.dl(e.detail.code)
					
					
					// uni.request({
					// 	url: this.baseUrl + '/system/getPhoneNoInfo',
					// 	method: 'POST',
					// 	dataType: 'json',
					// 	data: {
					// 		encryptedData: e.detail.encryptedData,
					// 		iv: e.detail.iv,
					// 		sessionKey: this.sessionKey,
					// 	},
					// 	success: (res) => {
					// 		console.log(res);
					// 		uni.hideLoading();
					// 		if (res.data.code == 200) {


					// 		} else {
					// 			this.$refs.uToast.show({
					// 				title: res.data.message,
					// 				type: 'default',
					// 				icon: false
					// 			})
					// 		}
					// 	},
					// 	fail: (res) => {
					// 		uni.hideLoading();
					// 		this.$refs.uToast.show({
					// 			title: '服务器连接超时',
					// 			type: 'default',
					// 			icon: false
					// 		})
					// 	}
					// });


					//e.detail这里会有三个属性
					//encryptedData
					//errMsg    
					//iv
				} else {
					this.$refs.uToast.show({
						title: '请授权绑定手机号',
						type: 'default',
						icon: false
					})
					console.log('用户点击了拒绝');
				}
			},

			getCode() {
				if (this.timeOut != 60) return
				var reg = /^1[3-9][0-9]{9}$/;
				if (!reg.test(this.iphone)) {
					uni.showToast({
						title: '请输入有效手机号',
						duration: 5000,
						icon: 'none',
					});
					return
				}
				uni.showLoading({
					title: '发送中',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/system/getCode',
					method: 'POST',
					dataType: 'json',
					data: {
						PERTB006CA01: this.iphone
					},
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							uni.showToast({
								title: '发送成功',
								duration: 5000,
								icon: 'none',
							});
							this.countdown()
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
						uni.hideLoading();
					},
					fail: (res) => {
						this.$refs.uToast.show({
							title: '服务器连接超时',
							type: 'default',
							icon: false
						})
						uni.hideLoading();
					}
				});
			},
			countdown() {
				var time = setInterval(() => {
					if (this.timeOut == 0) {
						clearInterval(time)
						this.codetext = '获取验证码'
						this.timeOut = 60
					} else {
						var outTime = this.timeOut--
						if (this.timeOut < 10) {
							outTime = '0' + outTime
						}
						this.codetext = outTime + '秒后重新获取'
					}
				}, 1000)
			},
			getToken() {
				uni.request({
					url: this.baseUrl + '/system/token',
					method: 'GET',
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							this.token = res.data.data
						}
					},
					fail: (res) => {
						console.log(res);
						this.$refs.uToast.show({
							title: '服务器连接超时',
							type: 'default',
							icon: false
						})
					}
				});
			},
			submitLogin(code) {
				// var that = this

				// var reg = /^1[3-9][0-9]{9}$/;
				// if (!reg.test(this.iphone)) {
				// 	uni.showToast({
				// 		title: '请输入有效手机号',
				// 		duration: 5000,
				// 		icon: 'none',
				// 	});
				// 	return
				// }
				// if (!this.code && (this.code.length != 4 || this.code.length != 6)) {
				// 	uni.showToast({
				// 		title: '请输入有效验证码',
				// 		duration: 5000,
				// 		icon: 'none',
				// 	});
				// 	return
				// }



				



			},
			hqwxuse(){
				uni.getUserProfile({
					provider: "weixin",
					lang: "zh_CN",
					desc: "获取你的昵称、头像、地区及性别",
					success: (res) => {
						// console.log("user", res);
						this.userInfo = res.userInfo;
						// console.log(this.userInfo)
						
				
				
				
					},
					fail: err => {
						console.log("获取用户信息失败");
						this.$refs.uToast.show({
							title: '请同意授权',
							type: 'default',
							icon: false
						})
					}
				})
			},
			
			dl(code){
				uni.showLoading({
					title: '登录中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/system/wxLogin',
					method: 'POST',
					dataType: 'json',
					data: {
						PERTB005CA03: code,
						// PERTB006CA00: this.code,
						PERTB005CA11: uni.getStorageSync('openid'),
						ORMTB410CA00: this.nickName,
						token: this.token
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
				
							var str = JSON.stringify(res.data.data)
							uni.setStorageSync('userinfo', str);
							uni.setStorageSync('token', this.token);
							console.log(str)
							uni.switchTab({
								url: '/pages/tabbar/home/index'
							});
				
				
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
					},
					fail: (res) => {
						uni.hideLoading();
						this.$refs.uToast.show({
							title: '服务器连接超时',
							type: 'default',
							icon: false
						})
					}
				});
			},

			submitykLogin() {
				var that = this
				uni.showLoading({
					title: '登录中...',
					mask: true
				});
				uni.switchTab({
					url: '/pages/tabbar/home/index'
				});
				uni.hideLoading();
			},

			iphoneIpt() {
				var regP = /^1[3-9][0-9]{9}$/;
				if (regP.test(this.iphone)) {
					uni.request({
						url: this.baseUrl + '/system/checkPhone/' + this.iphone,
						method: 'GET',
						success: (res) => {
							console.log(res)
							if (res.data.code == 201) {
								uni.reLaunch({
									url: '/page/registered/index?iphone=' + this.iphone + '&token=' +
										this.token
								})
							} else if (res.data.code != 200 && res.data.code != 201) {
								this.$refs.uToast.show({
									title: res.data.message,
									type: 'default',
									icon: false
								})

							}
						},
						fail: (res) => {
							uni.hideLoading();
							this.$refs.uToast.show({
								title: '服务器连接超时',
								type: 'default',
								icon: false
							})
						}
					});
				}
			}
		}
	}
</script>

<style scoped>
	.login-top {
		width: 45%;
		margin: 160rpx auto 40rpx auto;
	}

	.login-top-image {
		width: 200rpx;
		height: 200rpx;
		margin: 0rpx auto;
	}

	.login-top-image image {
		width: 200rpx;
		height: 200rpx;
	}

	.login-top-title {
		margin-top: 20rpx;
		font-size: 32px;
		font-family: Source Han Sans SC;
		font-weight: 500;
		color: #00A2F2;
		text-align: center;
	}

	.login-top-title-text {
		margin-top: 10rpx;
		font-size: 28rpx;
		font-family: Source Han Sans SC;
		color: #666666;
		text-align: center;
	}

	.login-content {
		padding: 60rpx 60rpx 0;
	}

	.login-li {
		display: flex;
		/* border-bottom: 1px solid #e3e3e3; */
		margin-top: 20rpx;
		font-size: 26rpx;
		/* padding-left: 20rpx; */
	}

	.login-li text {
		width: 40rpx;
		height: 60rpx;
		line-height: 60rpx;
	}

	.login-li input {
		flex: 1;
		height: 60rpx;
		line-height: 60rpx;
	}

	.login-li view {
		text-align: center;
		width: 260rpx;
		height: 60rpx;
		line-height: 60rpx;
		font-size: 24rpx;
		color: rgba(0, 168, 242, 1);
	}

	.login-li+.login-li {
		margin-top: 40rpx;
	}

	.login-li .iconfont {
		color: rgba(178, 178, 178, 1)
	}

	.login-content-btn {
		background-color: rgba(0, 122, 255, 1);
		color: #FFF;
	}

	.login-content-btn-active {
		background-color: rgba(0, 122, 255, .5);
	}

	.login-button {
		margin: 170rpx 40rpx 0;
	}
	.wx-nickName{
		border-bottom:2rpx solid #e3e3e3;
		padding: 0;
	}
	.wx-nickName .weui-input{
		color: #000000;
		margin: 0 auto;
	}
</style>
